﻿using Client.Framework;
using UnityEngine;
using UnityEngine.UI;

[DisallowMultipleComponent]
[RequireComponent(typeof(Button))]
public class ToggleBtn : MonoBehaviour
{
    [HideInInspector]
    public int idx = 0;
    public Text tabName;

    private Button button;
    private ToggleCtrl ctrl;

    public GameObject enableObj;
    public GameObject disableObj;
    public GameObject selectObj;
    public GameObject redPointObj;

    void Awake()
    {
        ctrl = GetComponentInParent<ToggleCtrl>();
        button = gameObject.GetComponent<Button>();
        if (button) { button.onClick.AddListener(OnPointerClick); }
    }

    void OnDestroy()
    {
        if (button) { button.onClick.RemoveListener(OnPointerClick); }
    }

    void OnPointerClick()
    {
        if (ctrl) { ctrl.OnTabClicked(idx); }
    }

    public void SetTabActive(bool bActive)
    {
        Utility.SetActive(gameObject, bActive);
    }

    public void SetTabEnable(bool bActive)
    {
        Utility.SetActive(selectObj, bActive);
        Utility.SetActive(enableObj, bActive);
        Utility.SetActive(disableObj, !bActive);
    }

    public void SetTabRedPoint(bool bActive)
    {
        Utility.SetActive(redPointObj, bActive);
    }

    public void SetTabName(string name)
    {
        if (tabName) { tabName.text = name; }
    }

    public void SetTabState(bool enabled)
    {
        if (button != null) { button.enabled = enabled; }
    }
}